<html>
    <script>        
        var Map = function(){
            this._entrys = new Array();
            this.put = function(key, value){
                if(key == null || value == null){
                    return;
                }
                let index = this._getIndex(key);
                if(index == -1){
                    let entry = new Object();
                    entry.key = key;
                    entry.value = value;
                    this._entrys[this._entrys.length] = entry;
                } else {
                    this._entrys[index].value = value;
                }
            }
            this.get = function(key){
                let index = this._getIndex(key);
                return (index!=-1)?this._entrys[index].value:null;
            }
            this.remove = function(key){
                let index = this._getIndex(key);
                if(index != -1){
                    this._entrys.splice(index, 1);
                }
            }
            this.clear = function(){
                this._entrys.length = 0;
            }
            this.contains = function(key){
                let index = this._getIndex(key);
                return index != -1;
            }
            this.size = function(){
                return this._entrys.length;
            }
            this.getEntrys = function(){
                return this._entrys;
            }
            this._getIndex = function(key){
                if(key != null && key != undefined){
                    let _lenght = this._entrys.length;
                    for(let i = 0; i < _lenght; i++){
                        let entry = this._entrys[i];
                        if(entry == null || entry == undefined){
                            continue;
                        }
                        if(entry.key == key){
                            return i;
                        }
                    }
                }
                return -1;
            }
        }

        function Client(uid, conn, roomId){
            this.uid = uid; // 用户唯一标识
            this.conn = conn;   // WebSocket连接
            this.roomId = roomId;   // 房间号
            console.log(`uid: ${uid}, conn: ${conn}, roomId: ${roomId}`);
        }
        
        var map = new Map();
        var uid = new Date().getTime().toString(36); // 时间戳转三十六进制方式生成唯一编号，如：lswte2aq
        var client = new Client(uid, 100, 100);
        map.put(uid, client);
        var uid2 = new Date().getTime().toString(36); // 时间戳转三十六进制方式生成唯一编号，如：lswte2aq
        var client2 = new Client(uid2, 100, 100);
        map.put(uid2, client2);
        console.log(map.size());
        map.remove(uid);
        console.log(map.size());

        // Promise 测试
        {
            function taskA(){
                console.log('task a');
                //throw new Error('task a 出现了异常');
            }
            function taskB(){
                console.log('task b');
                throw new Error('task b 出现了异常');
            }
            function onRejected(error){
                console.log('on rejected catch error: A or B', error);
            }
            function finalTask(){
                console.log('final task');
            }

            Promise.resolve().then(taskA)
                .then(taskB)    // taskA 如果出现异常，taskB 不会执行
                .catch(onRejected)  // 只有 taskA 或 taskB 出现异常，才会进入 onRejected
                .then(finalTask);   // finalTask 始终会执行

            console.log('观察 Promise 是不是异步执行');
        }

    </script>
</html>